package LeetCode.leetcode142;

public class Solution {
    public ListNode detectCycle(ListNode head) {
        ListNode slow = head;
        ListNode fast = head;
        while (fast != null && fast.next != null) {
            slow = slow.next;
            fast = fast.next.next;
            //说明存在环形
            if (slow == fast) {
                break;
            }
        }
        if (fast == null || fast.next == null) {return null;}
        fast = head;
        while (slow != fast) {
            slow = slow.next;
            fast = fast.next;
        }
        return  slow;

    }
}